import type { BingElements } from '../../../../../../types/constants/personalization/bing'
import type { ThemeType } from '../../../../../../types/interfaces'
import { SearchThemeService } from '../../../../../../lib/abstract'
import { PageHandler } from '../../../../../../lib/page_handler'
import { BingPageType } from '../../../../../../lib/page_handler/platform/bing/types'
import {
  UNDEFINED_PAGE_TYPE,
  UNDEFINED_PAGE_TYPE_TIPS,
} from '../../../../../../lib/page_handler/type'
import {
  BODY_SELECTOR,
  logger,
  loggerDeduplicator,
  PLATFORM_PAGE_TYPES,
  SUPPORTED_PLATFORMS,
} from '../../../../../../types/constants'
import { BingIndexPage } from './bing/page'
import { BingNotFoundPage } from './bing/page/404'
import { BingAcademicPage } from './bing/page/academic'
import { BingCommonPage } from './bing/page/common'
import { BingDictionaryPage } from './bing/page/dictionary'
import { BingVideoPage } from './bing/page/video'
import { BingWebPage } from './bing/page/web'

/**
 * 页面处理器
 */
const PAGE_HANDLERS: Partial<Record<BingPageType, any>> = {
  [BingPageType.Index]: BingIndexPage,
  [BingPageType.Web]: BingWebPage,
  [BingPageType.Video]: BingVideoPage,
  [BingPageType.Academic]: BingAcademicPage,
  [BingPageType.Dictionary]: BingDictionaryPage,
  [BingPageType.NotFound]: BingNotFoundPage,
}

/**
 * 主题服务测试链接
 */
export class BingThemeService extends SearchThemeService {
  readonly name: string = 'BingTheme'
  readonly platformPageType: string = PLATFORM_PAGE_TYPES.SEARCH.value
  private static instance: BingThemeService
  declare readonly elements: BingElements
  private readonly commonPage: BingCommonPage = BingCommonPage.getInstance()
  private readonly pageHandler: PageHandler = PageHandler.getInstance()
  private currentPageType: BingPageType | 'default' = this.pageHandler.pages.bing.pageType
  readonly wrapperSelector: string = BODY_SELECTOR

  constructor() {
    super(SUPPORTED_PLATFORMS.BING)

    this.updateElementStylesForTheme()
    this.checkPageType(this.currentPageType)
  }

  /**
   * 获取单例实例
   */
  static getInstance(): BingThemeService {
    if (!BingThemeService.instance) {
      BingThemeService.instance = new BingThemeService()
    }
    return BingThemeService.instance
  }

  /**
   * 批量初始化页面样式
   */
  updateElementStylesForTheme(): void {
    // 通用样式初始化
    this.commonPage.init()

    // 动态获取当前页面处理器并初始化
    const PageHandlerClass = PAGE_HANDLERS[this.currentPageType as BingPageType]
    if (PageHandlerClass) {
      PageHandlerClass.getInstance().init()
    }
    else if (this.currentPageType === UNDEFINED_PAGE_TYPE) {
      loggerDeduplicator.warn(UNDEFINED_PAGE_TYPE_TIPS)
    }
    else {
      logger.error(`未找到 ${this.currentPageType} 页面的处理器`)
    }
  }

  platformStyleVar(_theme: ThemeType): string {
    return ``
  }

  getBaseStyles(_theme: ThemeType): string {
    return `
${super.getBaseStyles(_theme)}
`
  }

  getPageStyles(_theme: ThemeType): string {
    return `
${BODY_SELECTOR} {
  background-color: var(${this.themeVarService.search.default.background}) !important;
  color: var(${this.themeVarService.search.default.text}) !important;
}
`
  }

  getMainStyles(theme: ThemeType): string {
    // 1. 通用样式
    let commonStyles = this.commonPage.getMainStyles()
    const notSupportedPages: BingPageType[] = [
      BingPageType.NotFound,
    ]

    if (notSupportedPages.includes(this.currentPageType as BingPageType))
      commonStyles = ''

    // 2. 动态获取当前页面样式
    let pageStyles = ''
    const PageHandlerClass = PAGE_HANDLERS[this.currentPageType as BingPageType]
    if (PageHandlerClass) {
      pageStyles = PageHandlerClass.getInstance().getMainStyles(theme)
    }

    return `
  /* ===================== 【页面】通用样式 ===================== */   
  ${commonStyles}
  /* ===================== 【页面】独立样式 ===================== */
  ${pageStyles}
`
  }

  getPatchedStyles(_theme: ThemeType): string {
    return ``
  }

  getFixedStyles(_theme: ThemeType): string {
    return ``
  }
}
